package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.PortletDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.PortletBean;

public class PortletDaoImpl extends BaseDao implements PortletDao {

	private static final Logger logger = Logger.getLogger(PortletDaoImpl.class);
			
	@Override
	public void changePortlet(String themeId, List<PortletBean> porletList)
			throws BaseException {
		try {
			sqlMapClient.startTransaction();
			sqlMapClient.getCurrentConnection().setAutoCommit(false);
			sqlMapClient.update("admin-portlet.delPortlet", themeId);
			for (PortletBean portletBean : porletList) {
				sqlMapClient.update("admin-portlet.createPortlet", portletBean);
			}
			sqlMapClient.getCurrentConnection().commit();
			logger.info("模板管理-改变首页模块|#[主题编号：" + themeId
					+ "],成功!");
		} catch (Exception e) {
			try {
				sqlMapClient.getCurrentConnection().rollback();
			} catch (SQLException e1) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
			logger.error("模板管理-改变首页模块|#[主题编号：" + themeId
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.portlet.change.error");
		} finally {
			try {
				sqlMapClient.getCurrentConnection().setAutoCommit(true);
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PortletBean> queryPortlet(Map<String, Object> paramMap)
			throws BaseException {
		List<PortletBean> portletLs = null;
		try {
			portletLs = sqlMapClient.queryForList("admin-portlet.queryPortlet",
					paramMap);
			logger.info("模板管理-查询首页模块|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-查询首页模块|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.portlet.query.error");
		}
		return portletLs;
	}

}
